package sn.hn.demo;

import sn.hn.demo.sound.Music;
import sn.hn.demo.thread.MyThreadFish;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class GameActivity extends Activity {
	public static int level = 0;
	public static Context mGameContext;
	public MyCustomDialog mDialog;
	RelativeLayout mGameLayout;
	public GameSurface mGameSurface;
	Handler mHandler;
	Thread mTimeThread;
	MyThreadFish mThreadFish;
	public static boolean running = true;
	Runnable runTime = new Runnable() {
		@Override
		public synchronized void run() {
			if (!mGameSurface.mFinish) {
				while (true) {
					try {
						Thread.sleep(1000);
						if(running)
							mGameSurface.mGameTime--;
					} catch (Exception e) {
					}
					if (mHandler != null)
						mHandler.sendEmptyMessage(0);
				}
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		mDialog = new MyCustomDialog(this);
		mGameContext = getBaseContext();
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		mGameSurface = new GameSurface(this);
		mThreadFish = new MyThreadFish(mGameSurface);
		mThreadFish.start();
		setContentView(mGameSurface);
		mHandler = new Handler() {
			@Override
			public synchronized void handleMessage(Message msg) {
				super.handleMessage(msg);
				if (mGameSurface.mGameTime == 0) {
					if (mGameSurface.mScore < mGameSurface.mLevel.getLevel_score()) {
						mGameSurface.mGameOver = true;
						mGameSurface.mFinish = true;
						showDialog(0);
					} else {
						mGameSurface.mGameOver = false;
						mGameSurface.mFinish = true;
					}
				}
				if (mGameSurface.mFinish) {
					if (!mGameSurface.mGameOver)
						showDialog(1);
				}
			}
		};
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = new MenuInflater(mGameContext);
		inflater.inflate(R.menu.options_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.pause:
			running = false;
			break;
		case R.id.resume:
			running = true;
		default:
			break;
		}
		return true;
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("Exit game");
			builder.setMessage("Do you want to exit?");
			builder.setPositiveButton("OK",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							finish();
						}
					});
			builder.setNegativeButton("Cancel", null);
			builder.show();
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		final Dialog dialog = new Dialog(GameActivity.this);
		if (id == 0) {
			dialog.setContentView(R.layout.dialog);
			dialog.getWindow().setLayout(MainActivity.SCREENWIDTH,
					MainActivity.SCREENHEIGHT);
			dialog.setTitle("Game Over");
			LayoutParams lp = dialog.getWindow().getAttributes();
			lp.alpha = 0.3f;
			dialog.getWindow().setAttributes(lp);
			Button btnRestart = (Button) dialog.findViewById(R.id.btnRestart);
			btnRestart.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					dialog.dismiss();
					mGameSurface = new GameSurface(getBaseContext());
					mThreadFish = new MyThreadFish(mGameSurface);
					mThreadFish.start();
					setContentView(mGameSurface);
				}
			});
			Button btnMoreGame = (Button) dialog.findViewById(R.id.btnMainMenu);
			btnMoreGame.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					dialog.dismiss();
					finish();
				}
			});
		}
		if (id == 1) {
			dialog.setTitle("Chúc mừng bạn đã qua màn chơi");
			dialog.setContentView(R.layout.congra);
			dialog.getWindow().setLayout(MainActivity.SCREENWIDTH,
					MainActivity.SCREENHEIGHT);
			dialog.setTitle("Chúc mừng!");
			LayoutParams lp = dialog.getWindow().getAttributes();
			lp.alpha = 0.3f;
			dialog.getWindow().setAttributes(lp);
			TextView tv = (TextView) dialog.findViewById(R.id.message);
			tv.setText("Nhấn nút để tiếp tục màn chơi tiếp theo");
			Button bt = (Button) dialog.findViewById(R.id.next);
			bt.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					dialog.dismiss();
					level = mGameSurface.mLevel.level;
					mGameSurface = new GameSurface(getBaseContext());
					mGameSurface.mLevel.level = level + 1;
					Log.d("level", ": " + mGameSurface.mLevel.level);
					mThreadFish = new MyThreadFish(mGameSurface);
					mThreadFish.start();
					setContentView(mGameSurface);
				}
			});
		}
		return dialog;
	}
	
	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onResume() {
		super.onResume();
		running = true;
		Music.play(this, R.raw.u);
	}

	@Override
	protected void onPause() {
		super.onPause();
		running = false;
		Music.stop(this);
	}
	
	@Override
	protected void onStop() {
		super.onStop();
	}
	
	@Override
	protected void onDestroy() {
		mTimeThread = null;
		mHandler = null;
		super.onDestroy();
	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		if (mTimeThread == null) {
			mTimeThread = new Thread(runTime);
			mTimeThread.start();
		}
	}
}
